﻿using System.Diagnostics;
using System.Text.RegularExpressions;

namespace RSSReader
{
    class Utils
    {
        public static int RunShell(string command, string arguments, out string output)
        {
            output = "";
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.FileName = command;
            p.StartInfo.Arguments = arguments;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.Start();
            //// Do not wait for the child process to exit before
            //// reading to the end of its redirected stream.
            //// p.WaitForExit();
            //// Read the output stream first and then wait.
            //output = p.StandardOutput.ReadToEnd();
            //output += p.StandardError.ReadToEnd();
            p.WaitForExit();

            return p.ExitCode;
        }

        public static string CleanName(string name)
        {
            return name.Replace("-", " ").Replace(".", " ").Replace("[", " ").Replace("]", " ").Replace("\n", "").Replace("  ", " ").Replace("HD 720p: ","");
        }

        public static string CleanRssItemTitle(string title)
        {
            string newTitle = title;

            if (newTitle.Length < 1)
                return newTitle;

            newTitle = newTitle.Trim();

            int slashPos = newTitle.IndexOfAny(new char[]{'/','-'});
            if (slashPos > 0)
            {
                // אבגד / ABCD => ABCD
                bool found = false;
                for (int i = 0; i < slashPos; i++)
                {
                    if (newTitle[i] >= 'א' && newTitle[i] <= 'ת')
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    newTitle = newTitle.Substring(slashPos + 1);
                }

                // ABCD / אבגד => ABCD
                found = false;
                for (int i = slashPos; i < newTitle.Length - slashPos; i++)
                {
                    if (newTitle[i] >= 'א' && newTitle[i] <= 'ת')
                    {
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    newTitle = newTitle.Substring(0, slashPos );
                }
            }

            newTitle = newTitle.Replace("(", "");
            newTitle = newTitle.Replace(")", "");
            newTitle = newTitle.Replace("/", "");
            newTitle = newTitle.Replace("\\", "");
            newTitle = newTitle.Replace("  ", " ");

            return newTitle.Trim();
        }

        public static SeriesEpisode GetSeriesEpisodeUsingSeasonEpisodePattern(string title, out int titleEndPos)
        {
            titleEndPos = -1;
            SeriesEpisode seriesEpisode = null;
            Regex sxe = new Regex(@".*(season)\s*[:]{0,1}\s*([0-9]{1,2}).*episode\s*[:]{0,1}\s*([0-9]{1,2}).*", RegexOptions.IgnoreCase);
            Match m = sxe.Match(title);
            if (m.Success)
            {
                int season = int.Parse(m.Groups[2].Value);
                int episode = int.Parse(m.Groups[3].Value);
                titleEndPos = m.Groups[1].Index;
                seriesEpisode = new SeriesEpisode(title, season, episode);
            }

            return seriesEpisode;
        }

        public static SeriesEpisode GetSeriesEpisodeUsingSXEPattern(string title, out int titleEndPos)
        {
            SeriesEpisode seriesEpisode = null;
            titleEndPos = -1;

            Regex sxe = new Regex(".*([0-9]{1,2})[xX]([0-9]{1,2}).*");
            Match m = sxe.Match(title);
            if (m.Success)
            {
                int season = int.Parse(m.Groups[1].Value);
                int episode = int.Parse(m.Groups[2].Value);
                titleEndPos = m.Groups[1].Index;
                seriesEpisode = new SeriesEpisode(title, season, episode);
            }

            return seriesEpisode;
        }

        public static SeriesEpisode GetSeriesEpisodeUsingSEPattern(string title, out int titleEndPos)
        {
            titleEndPos = -1;
            SeriesEpisode seriesEpisode = null;
            Regex sxe = new Regex(".*([Ss])([0-9]{1,2})[eE]([0-9]{1,2}).*");
            Match m = sxe.Match(title);
            if (m.Success)
            {
                int season = int.Parse(m.Groups[2].Value);
                int episode = int.Parse(m.Groups[3].Value);
                titleEndPos = m.Groups[1].Index;
                seriesEpisode = new SeriesEpisode(title, season, episode);
            }

            return seriesEpisode;
        }

        //public static SeriesEpisode GetSeriesEpisodeUsingHebPattern(string title, out int titleEndPos)
        //{
        //    SeriesEpisode seriesEpisode = null;
        //    titleEndPos = -1;


        //    const string episodeName = "פרק";
        //    const string seriesName = "עונה";
        //    //Regex sxe = new Regex(".*([0-9]{1,2}).*" + episodeName + ".*([0-9]{1,2}).*" + seriesName + ".*");
        //    Regex sxe = new Regex("(.*):.*" + seriesName + ".*([0-9]{1,2}).*" + episodeName + ".*([0-9]{1,2}).*");
        //    Match m = sxe.Match(title);
        //    if (m.Success)
        //    {
        //        int season = int.Parse(m.Groups[3].Value);
        //        int episode = int.Parse(m.Groups[2].Value);
        //        titleEndPos = m.Groups[1].Index + m.Groups[1].Value.Length;
        //        seriesEpisode = new SeriesEpisode(title, season, episode);
        //    }

        //    return seriesEpisode;
        //}

        public static string GetQualityString(string quality)
        {
            if (quality.Equals("HD (1080/720/HDRIP/BDRIP)"))
                return "720|1080|HDRIP|BDRIP";
            else if (quality.Equals("DVD (DVDRIP/divx/xvid)"))
                return "DVDRIP|divx|xvid";
            else if (quality.Equals("Screener"))
                return "SCR";
            else if (quality.Equals("Cam"))
                return "CAM|KAM";
            else return "";
        }
    }


}
